package aries;

import aries.database.Database;
import java.io.File;
import java.util.ArrayList;

public class Configuration {

    /**
     * Name of the configuration file.
     */
    private String filename = "conf.ini";
    
    /**
     * Title.
     */
    private ArrayList<String> title = new ArrayList<String>();
    
    /**
     * Value.
     */
    private ArrayList<String> value = new ArrayList<String>();
    
    /**
     * Database parameter for assign method.
     */
    public static final byte DATABASE = 0;

    /**
     * Create new saved configuration.
     * The name of your configuration file is <code>conf.ini</code>
     */
    public Configuration(ArrayList<String> title, ArrayList<String> value) {
        if (this.title.size() < 1) {
            this.title = title;
            this.value = value;
        } else {
            for (byte i = 0; i < title.size(); i++) {
                this.title.add(title.get(i));
                this.value.add(value.get(i));
            }
        }
    }

    /**
     * Create new saved database configuration.
     * 
     * @param filename 
     */
    public Configuration(String filename, ArrayList<String> title, ArrayList<String> value) {
        if (this.title.size() < 1) {
            this.filename = filename;
            this.title = title;
            this.value = value;
        } else {
            for (byte i = 0; i < title.size(); i++) {
                this.title.add(title.get(i));
                this.value.add(value.get(i));
            }
        }
    }

    /**
     * Create new saved database configuration.
     * 
     * @param filename 
     */
    public Configuration(File filename, ArrayList<String> title, ArrayList<String> value) {
        if (this.title.size() < 1) {
            this.filename = filename.getName();
            this.title = title;
            this.value = value;
        } else {
            for (byte i = 0; i < title.size(); i++) {
                this.title.add(title.get(i));
                this.value.add(value.get(i));
            }
        }
    }

    /**
     * Get title of the configuration.
     * 
     * @return ArrayList
     */
    public ArrayList<String> getTitle() {
        return title;
    }

    /**
     * Get value of the configuration.
     * 
     * @return ArrayList
     */
    public ArrayList<String> getValue() {
        return value;
    }

    /**
     * Add other configuration.
     * 
     * @param conf 
     */
    public void addOtherConfiguration(Configuration conf) {
        this.title = conf.getTitle();
        this.value = conf.getValue();
    }
    
    /**
     * Generate configuration file.
     * 
     * @throws Exception 
     */
    public void generate() throws Exception {
        Ini ini = new Ini(filename);
        ini.create(title.toArray(), value.toArray(), "Aries database configuration file.");
    }
    
    /**
     * Assign value from your database configuration file into system setting.
     * 
     * @throws Exception 
     */
    public void assign(byte type) throws Exception {
        Ini ini = new Ini(filename);
        if (type == DATABASE) {
            Database.setIp(ini.getValue("ip"));
            Database.setPort(ini.getValue("port"));
            Database.setDbName(ini.getValue("dbName"));
            String user = ini.getValue("user");
            String pass = ini.getValue("password");
            /*@TODO : handle encryption
            if (Setting.getUseEncrypt()) {
                    user = new AES(ini.getValue("user")).decrypt(Setting.key);
                    pass = new AES(ini.getValue("password")).decrypt(Setting.key);
            }*/
            Database.setUser(user);
            Database.setPass(pass);
            Database.setProduct(Byte.parseByte(ini.getValue("product")));
        }
    }
    
}
